
.CODE             ;Indicates the start of a code segment.


;void OrR(void *R1, const void *R2, unsigned maxX)
	public	OrR
OrR proc \
        uses rsi rdi,
;	R1:ptr byte,	RCX
;       R2:ptr byte,	RDX
;       maxX:DWORD	R8

        mov     rdi,rcx
        mov     rsi,rdx
        mov	rcx,R8

        mov     rcx,R8		; cx=amount of pixels
	sub	rcx,8
	jl	LoopSimple

		; Processing of 4px block with size 3xDWORD	
LoopPix4:lodsq
	or	[rdi],rax
	add	rdi,8
	sub	rcx,8
	jae	LoopPix4

			; Simple loop for 0,1,2,3 pixels (works also for more px)
LoopSimple:
	add	rcx,8		; ecx was -4, correct counter
        jz	ToEnd		; array has zero size
LoopPix:lodsb
        or	[rdi],al
	inc	rdi
	loop	LoopPix
ToEnd:	ret
	
OrR endp


;void AndR(void *R1, const void *R2, unsigned maxX)
	public	AndR
AndR proc \
        uses rsi rdi
;	R1:ptr byte,	RCX
;       R2:ptr byte,	RDX
;       maxX:DWORD	R8

        mov     rdi,rcx
        mov     rsi,rdx
        mov	rcx,R8

        mov     rcx,R8		; cx=amount of pixels
	sub	rcx,8
	jl	LoopSimple

		; Processing of 4px block with size 3xDWORD	
LoopPix4:lodsq
	and	[rdi],rax
	add	rdi,8
	sub	rcx,8
	jae	LoopPix4

			; Simple loop for 0,1,2,3 pixels (works also for more px)
LoopSimple:
	add	rcx,8		; ecx was -4, correct counter
        jz	ToEnd		; array has zero size
LoopPix:lodsb
        and	[rdi],al
	inc	rdi
	loop	LoopPix
ToEnd:	ret
	
AndR endp



;void AddLu32u8(unsigned Size, uint32_t *Accu, uint8_t *pAdd)
	public  AddLu32u8
AddLu32u8 proc \
        uses rsi
;       count:DWORD,	RCX
;       Accu: ptr byte, RDX
;       pAdd: ptr byte R8
        
        mov	rsi,R8
        or	rsi,rsi
        jz	ToEnd
        or	rdx,rdx
        jz	ToEnd
        jrcxz	ToEnd

ByteLop:movzx	eax,byte ptr [rsi]
	inc	rsi
	add	[rdx],eax
	add	rdx,4
	loop	ByteLop
ToEnd:
        ret                     ; _cdecl return        
        
AddLu32u8 endp



;void SubLu32u8(unsigned Size, uint32_t *Accu, uint8_t *pSub)
	public  SubLu32u8
SubLu32u8 proc \
        uses rsi
;       count:DWORD,	RCX
;       Accu: ptr byte, RDX
;       pAdd: ptr byte R8
        
        mov	rsi,R8
        or	rsi,rsi
        jz	ToEnd
        or	rdx,rdx
        jz	ToEnd
        jrcxz	ToEnd

ByteLop:movzx	eax,byte ptr [rsi]
        inc	rsi
	sub	[rdx],eax
	add	rdx,4
	loop	ByteLop
ToEnd:
        ret                     ; _cdecl return        
        
SubLu32u8 endp


	end

